package com.cpi.cornpp.transports.action;



import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportQueryForm;
import com.cpi.cornpp.transports.service.TransportService;

public class ShowTransportsQueryAction extends Action {
	private static final String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException{
		int orderField = 0;
		int orderType = 0;
		boolean ascendingOrder = true;
		String messageError = null;
		TransportQueryForm transportQueryForm = null;
		TransportService transportsService = null;
		List transportList = null;
		HttpSession httpSession = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		httpSession = request.getSession(false);
		
		try{
			
			if (form != null){
				transportQueryForm = (TransportQueryForm) form;
				messageResources = getResources(request);
				
				transportsService = new TransportService();
				
				orderField = (StringUtils.isNotEmpty(transportQueryForm.getOrderField())) ? Integer.parseInt(transportQueryForm.getOrderField()) : 1;
				orderType = (StringUtils.isNotEmpty(transportQueryForm.getOrderType())) ? Integer.parseInt(transportQueryForm.getOrderType()) : 0;
				
				try{
					
					ascendingOrder = (1 == orderType) ? false : true;
					
					transportList = transportsService.getTransports(orderField, ascendingOrder);
					
				} catch(TransportException tre){
					messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
					if(null == messageError){
						form.reset(mapping, request);
						throw tre;
					}else{
						httpSession = request.getSession(false);
						httpSession.setAttribute(ERROR_MESSAGE, messageError);
					}
				}
			}
			
			httpSession.setAttribute("previousOrderField", Integer.toString(orderField));
			httpSession.setAttribute("previousOrderType", Integer.toString(orderType));
			request.setAttribute("transportList", transportList);
			
			forward = mapping.findForward(SUCCESS_FORWARD);
			
		} catch(Exception e){
			TransportException tre = new TransportException(e, TransportException.LAYER_ACTION,CornPPException.ACTION_LISTS);;
			
			e.printStackTrace(System.out);
			form.reset(mapping, request);
			
			throw tre;
		} finally {
			httpSession.removeAttribute("deletedTransportCostElements");
			httpSession.removeAttribute("addedTransportCostElements");
			httpSession.removeAttribute("transportView");
			httpSession.removeAttribute("costElementsList");
		}
		
		return forward;
	}
}